package com.wjw.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebParam.Mode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wjw.bean.AjaxResult;
import com.wjw.bean.Page;
import com.wjw.bean.Role;
import com.wjw.bean.User;
import com.wjw.dao.RoleDao;
import com.wjw.dao.UserDao;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserDao userdao;
	@Autowired
	private RoleDao roledao;
	
	@RequestMapping("index1")
	public String index(@RequestParam(required=false,defaultValue="1")Integer pagenum,
			@RequestParam(required=false,defaultValue="2")Integer pagesize,Model model){
		Map<String,Object> mp=new HashMap();
		mp.put("start", (pagenum-1)*pagesize);
		mp.put("size", pagesize);
		List<User> lu=userdao.pagefind(mp);
		model.addAttribute("users",lu);
		return "user/index";
	}
	
	@RequestMapping("index")
	public String toindex(){
		return "user/index";
	}
	
	@RequestMapping("pagefind")
	@ResponseBody
	public Object ajaxpagefind(String searchtext,Integer pagenum,Integer pagesize,Model model){
		AjaxResult ar=new AjaxResult();
		Page<User> pageuser=new Page<>();
		try {
			Map<String,Object> mp=new HashMap();
			mp.put("start", (pagenum-1)*pagesize);
			mp.put("size", pagesize);
			mp.put("searchtext", searchtext);
			List<User> lu=userdao.pagefind(mp);
			int pageNo=userdao.findPageNum(mp);
			int pageMax=0;
			if(pageNo%pagesize==0){
				pageMax=pageNo/pagesize;
			}else{
				pageMax=pageNo/pagesize+1;
			}
			
			pageuser.setDatas(lu);
			pageuser.setPageno(pagenum);
			pageuser.setTotalsize(pageNo);
			pageuser.setTotalno(pageMax);
			System.out.println(pageuser.getTotalno());
			ar.setData(pageuser);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		System.out.println(pageuser.getTotalno());
		return ar;
	}
	
	@RequestMapping("add")
	public String toadd(){
		return "user/add";
	}
	
	@RequestMapping("insert")
	@ResponseBody
	public Object insert(User user){
		AjaxResult ar=new AjaxResult();
		System.out.println(user.getLoginacct());
		try {
			
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			user.setCreatetime(sdf.format(new Date()));
			user.setUserpwd("123");
			userdao.insertUser(user);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
	
	@RequestMapping("edit")
	public String edit(Integer id,Model model){
		
		User user=userdao.findById(id);
		model.addAttribute("user",user);
		return "user/edit";
	}
	
	@RequestMapping("update")
	@ResponseBody
	public Object update(User user){
		AjaxResult ar=new AjaxResult();
		try {
			userdao.updateUser(user);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public Object delete(Integer id){
		AjaxResult ar=new AjaxResult();
		try {
			userdao.deleteUser(id);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
	
	@RequestMapping("deletes")
	@ResponseBody
	public Object deletes(Integer[] userid){
		
		AjaxResult ar=new AjaxResult();
		try {
			Map<String, Object> map=new HashMap<>();
			map.put("userids", userid);
			userdao.deleteUsers(map);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
	
	@RequestMapping("assign")
	public String assign(Integer id,Model model){
		User user=userdao.findById(id);
		model.addAttribute("user",user);
		List<Role> lr=roledao.findAll();
		
		List<Role> leftList=new ArrayList<>();
		List<Role> rightList=new ArrayList<>();
		List<Integer> roleids=userdao.findRoleByUser(id);
		for(Role role:lr){
			if(roleids.contains(role.getId())){
				rightList.add(role);
			}else{
				leftList.add(role);
			}
		}
		model.addAttribute("leftRoles", leftList);
		model.addAttribute("rightRoles", rightList);
		return "user/assign";
	}
	
	@RequestMapping("addassign")
	@ResponseBody
	public Object addoassign(Integer userid,Integer[] leftids){
		AjaxResult ar=new AjaxResult();
		try {
			Map<String, Object> map=new HashMap<>();
			map.put("userid", userid);
			map.put("roleids", leftids);
			userdao.insertUserRoles(map);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
	
	@RequestMapping("delassign")
	@ResponseBody
	public Object delassign(Integer userid,Integer[] rightids){
		AjaxResult ar=new AjaxResult();
		try {
			Map<String, Object> map=new HashMap<>();
			map.put("userid", userid);
			map.put("roleids", rightids);
			userdao.deleteUserRoles(map);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
}
